package com.baijia.web.controller.internal;

import com.baijia.common.dto.request.ChangePwdBySmsCodeRequest;
import com.baijia.common.dto.request.ChangePwdRequest;
import com.baijia.common.dto.request.IsRegistedUserRequest;
import com.baijia.common.dto.response.InternalResponse;
import com.baijia.common.enums.ResponseStatus;
import com.baijia.services.IAccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping(value = "/internal")
@Slf4j
public class InternalServiceController {

    @Autowired
    private IAccountService accountService;

    @RequestMapping(value = "/isRegistedUser.do")
    @ResponseBody
    public InternalResponse<Map<String, Object>> isRegistedUser(HttpServletRequest request,
        IsRegistedUserRequest isRegistedUserRequest) {

        log.info("isRegistedUserRequest : {}", isRegistedUserRequest);

        InternalResponse<Map<String, Object>> response = null;
        if (!isRegistedUserRequest.isValidRequest()) {
            response = new InternalResponse<>();
            response.setCode(ResponseStatus.IS_PARAM_INVALID.getCode())
                .setMsg(ResponseStatus.IS_PARAM_INVALID.getMsg());
            return response;
        }

        try {
            return accountService.isRegisted(isRegistedUserRequest);
        } catch (Exception e) {
            log.error("InternalServiceController.isRegistedUser : execution failed", e);
            response = new InternalResponse<>();
            response.setCode(ResponseStatus.IS_EXEC_FAILED.getCode()).setMsg(ResponseStatus.IS_EXEC_FAILED.getMsg());
            return response;
        }
    }

    @RequestMapping(value = "/changePassword.do")
    @ResponseBody
    public InternalResponse<Map<String, Object>> changePassword(HttpServletRequest request,
        ChangePwdRequest changePwdRequest) {

        InternalResponse<Map<String, Object>> response = null;
        log.info("changePwdRequest : {}", changePwdRequest);

        try {

            if (!changePwdRequest.isValidRequest()) {
                response = new InternalResponse<>();
                response.setCode(ResponseStatus.IS_PARAM_INVALID.getCode()).setMsg(
                    ResponseStatus.IS_PARAM_INVALID.getMsg());
                return response;
            }

            return accountService.updatePassword(changePwdRequest);
            // NOTE 调用方清除登录状态
        } catch (Exception e) {
            log.error("InternalServiceController.changePassword : execution failed", e);
            response = new InternalResponse<>();
            response.setCode(ResponseStatus.IS_EXEC_FAILED.getCode()).setMsg(ResponseStatus.IS_EXEC_FAILED.getMsg());
            return response;
        }

    }

    @RequestMapping(value = "/changePwdBySmsCode.do")
    @ResponseBody
    public InternalResponse<Map<String, Object>> changePwdBySmsCode(HttpServletRequest request,
        ChangePwdBySmsCodeRequest changePwdBySmsCodeRequest) {

        InternalResponse<Map<String, Object>> response = null;
        log.info("changePwdRequest : {}", changePwdBySmsCodeRequest);

        try {

            if (!changePwdBySmsCodeRequest.isValidRequest()) {
                response = new InternalResponse<>();
                response.setCode(ResponseStatus.IS_PARAM_INVALID.getCode()).setMsg(
                    ResponseStatus.IS_PARAM_INVALID.getMsg());
                return response;
            }

            return accountService.updatePwdBySmsCode(changePwdBySmsCodeRequest);
            // NOTE 调用方清除登录状态
        } catch (Exception e) {
            log.error("InternalServiceController.changePwdBySmsCode : execution failed", e);
            response = new InternalResponse<>();
            response.setCode(ResponseStatus.IS_EXEC_FAILED.getCode()).setMsg(ResponseStatus.IS_EXEC_FAILED.getMsg());
            return response;
        }

    }
}